﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Relation management"
		PageController.AuthorizedRoles =  Array("editor","publisher")
		pageController.RequiresAuthentication  =True
		PageController.PageCSSFiles = "main.css" 		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Relation management of Content </H2>
	
	<%	Page.Execute %>
	
	
	<%dgRelation%>	
	
	<br/>
	
	<%txtQuery%> <%cmdSearch%>
	<%objDataGrid%>	
	
	<HR>
	<br>
	<a href="Content.asp">Back</a>
	
<%End Function%>



<% 
	
	Dim objDataGrid
	Dim dgRelation
	
	
	Dim SortOrder
	
	Dim parentID
	Dim txtQuery
	Dim cmdSearch
		
	Public Function Page_Init()
			
	
		Set objDataGrid = New_ServerDataGrid("objDataGrid")
		Set dgRelation = New_ServerDataGrid("dgRelation")
				
		Set txtQuery = New_ServerTextBoxEX("txtQuery",100,40)
		Set cmdSearch = New_ServerButton("cmdSearch")
		
		Page.AutoResetScrollPosition = True		
	End Function

	Public Function Page_Controls_Init()	
		SortOrder = "updatetime"
		cmdSearch.Text = "Search"
		
		
		dgRelation.Pager.ProgressStyle = "font-size:8pt"
		dgRelation.Pager.PrevNextStyle = "font-size:8pt"
		dgRelation.Pager.PagerStyle    = "font-size:8pt"	
		dgRelation.AllowPaging = true
		dgRelation.Pager.PagerType = 0
		dgRelation.Pager.PagerSize = 20
		dgRelation.Pager.CurrentPageStyle = "color:blue;font-weight:bold"	

		dgRelation.AlternatingItemStyle = "background-color:#dddddd;font-size:8pt;border:1px solid black" 		
		dgRelation.SortStyle = "color:white"
		dgRelation.ItemStyle = "font-size:8pt;border:1px solid black"
		dgRelation.Control.Style = "width:100%;border-collapse:collapse;"
		dgRelation.HeaderStyle = "font-weight:bold;color:white;background-color:#4682b4;font-size:10pt;font-family:tahoma;border:1px solid black"
		dgRelation.BorderWidth = 1
		dgRelation.BorderColor = "Black"
		dgRelation.AutoGenerateColumns = False 'To avoid the grid control from doing this :-)
		
		
		
		SortOrder = "title"
		
		objDataGrid.Pager.ProgressStyle = "font-size:8pt"
		objDataGrid.Pager.PrevNextStyle = "font-size:8pt"
		objDataGrid.Pager.PagerStyle    = "font-size:8pt"	
		objDataGrid.AllowPaging = true
		objDataGrid.Pager.PagerType = 0
		objDataGrid.Pager.PagerSize = 20
		objDataGrid.Pager.CurrentPageStyle = "color:blue;font-weight:bold"	

		objDataGrid.AlternatingItemStyle = "background-color:#dddddd;font-size:8pt;border:1px solid black" 		
		objDataGrid.SortStyle = "color:white"
		objDataGrid.ItemStyle = "font-size:8pt;border:1px solid black"
		objDataGrid.Control.Style = "width:100%;border-collapse:collapse;"
		objDataGrid.HeaderStyle = "font-weight:bold;color:white;background-color:#4682b4;font-size:10pt;font-family:tahoma;border:1px solid black"
		objDataGrid.BorderWidth = 1
		objDataGrid.BorderColor = "Black"
		objDataGrid.AutoGenerateColumns = False 'To avoid the grid control from doing this :-)

	End Function
	
	
	Public Function cmdSearch_OnClick()
		GetContentPublished
	
	End Function
	
	
	Public Function Page_LoadViewState()
		SortOrder  = Page.ViewState.GetValue("SortOrder")	
		parentID = Page.ViewState.GetValue("parentID") 'Get from viewstate
	End Function

	Public Function Page_PreRender()
		Page.ViewState.Add "SortOrder",SortOrder
		Page.ViewState.Add "parentID",parentID
		
		GetContentRelation
		GetContentPublished
		
	End Function
	
	Public Sub GetContentRelation()		
		
		dgRelation.CreateColumns Array("id","title","description","createtime","username","categoryname","status", ""),Array("ID","Title","Description","Create Time","Create User","Category","Status", "Action")
	
		
		
		Set dgRelation.DataSource = GetRecordSet("Select c.id, c.title, c.description,c.createtime,c.usercreate,c.categoryid,cat.name as categoryname, c.status, u.username from content as c inner join category as cat on c.categoryid = cat.id " &_
		" right join contentrelation as cr on cr.childid=c.id " & _
		" inner join [user] as u on u.id = c.usercreate Where cr.parentid = " & parentID & " and c.isdeleted =0 Order By c." & SortOrder & " DESC")
		
		dgRelation.HeaderFunctionName = "dgRelation_Header"
	
		dgRelation.PagerStyle = "background-color:yellow"
	
		dgRelation.DataKeyFieldName = "id"
		dgRelation.Columns(7).ColumnType = 3    'Modify the column settings we need.
		dgRelation.Columns(7).HeaderText = "Actions"
		dgRelation.Columns(7).HorizonalAlign = "center"
		dgRelation.Columns(7).CellRenderFunctionName = "RenderActionColumn1"

	End Sub
	Public Sub GetContentPublished()		
		
		objDataGrid.CreateColumns Array("id","title","description","createtime","username","categoryname","status", ""),Array("ID","Title","Description","Create Time","Create User","Category","Status", "Action")
	

		Set objDataGrid.DataSource = GetRecordSet("Select c.id, c.title, c.description,c.createtime,c.usercreate,c.categoryid,cat.name as categoryname, c.status, u.username from content as c inner join category as cat on c.categoryid = cat.id " &_
		" inner join [user] as u on u.id = c.usercreate Where (c.title like '%"&txtQuery.Text&"%' or c.description like '%" & txtQuery.Text & "%') and c.id <> " & parentID & "  and c.status = 3 and  c.isdeleted =0  Order By c." & SortOrder & " DESC")
		objDataGrid.HeaderFunctionName = "objDataGrid_Header"
		'objDataGrid.FooterFunctionName = "objDataGrid_Footer"
		
	
		objDataGrid.PagerStyle = "background-color:yellow"
	
		objDataGrid.DataKeyFieldName = "id"
		objDataGrid.Columns(7).ColumnType = 3    'Modify the column settings we need.
		objDataGrid.Columns(7).HeaderText = "Actions"
		objDataGrid.Columns(7).HorizonalAlign = "center"
		objDataGrid.Columns(7).CellRenderFunctionName = "RenderActionColumn"

	End Sub
	

	Public Function objDataGrid_ClickColumn0(e)
		objDataGrid.SelectedItemIndex = CInt(e.Instance)
	End Function
	
	Public Function RenderColumn0(ds)
		 Response.Write " <A style='color:black' " 
		 Response.Write Page.GetEventScript("href","objDataGrid","ClickColumn0",ds.AbsolutePosition,"chris")
		 Response.Write " >" & ds(0) & "</a>"
	End Function
	
	Public Function RenderActionColumn1(ds)
	
		Response.Write "<A " & Page.GetEventScript("HREF","Page","DeleteRelation",ds("id").Value,"") & " Class='ActionLink'>Remove</a>"	
	
	End Function
	Public Function RenderActionColumn(ds)
	
		Response.Write "<A " & Page.GetEventScript("HREF","Page","AddRelation",ds("id").Value,"") & " Class='ActionLink'>Add</a>"	
	
	End Function
	
	Public Function Page_DeleteRelation(e)
		DBLayer.ExecuteSQL("Delete  from contentrelation where parentid = " & parentID & " and  childid=" & Replace(e.Instance,"'","''"))
		
	End Function
	Public Function Page_AddRelation(e)
		Dim rs
		Dim sql
		sql = "select * from contentrelation where parentid = " & parentID & " and childid = " & Replace(e.Instance,"'","''")
		set rs = DBLayer.GetRecordSet(sql)
		If rs.RecordCount = 0 Then
			rs.AddNew
		End if
		
		rs("parentid").Value = parentID
		rs("childid").Value = Replace(e.Instance,"'","''")
		
		DBLayer.UpdateRecordSet rs
		
	End Function
	
	Public Function Page_Load()
	
		If Not Page.IsPostBack Then	
			If Request.QueryString("parentID")<>"" Then
				parentID = Clng(Request.QueryString("parentID"))					
				 
			End if
		End If	
		
	End Function
	
	
	public Function objDataGrid_ColumnSort(e)
		If e.Instance = "id" Then
			SortOrder  = "id DESC"
		Else
			SortOrder  = "id"
		End If
		objDataGrid.SelectedItemIndex = -1
		objDataGrid.EditItemIndex= -1
	End Function
		
	Public Function RenderFormatedCell(v)
		If "" & v = "" Then
			RenderFormatedCell =  "@@"
		Else
			RenderFormatedCell =  v
		End If
	End Function
	
	
	
	Public Function objDataGrid_Header()
		Response.Write "List of all content have published"
	End Function
	Public Function dgRelation_Header()
		Response.Write "List of all relative content"
	End Function
	
	
%>